let app = new Vue({
    el: '#app',
    data: {
        user: JSON.parse(sessionStorage.getItem('user')),
        unreadNum: sessionStorage.getItem("unreadNum"),
        searchPaperType: null,
        searchSubjectName: null,
        examPapers: [],
        startNum:0,
        endNum:6,
        currPage:1,     //当前页
        pageSum:null    //总页数
    },
    created() {
        // 查询试卷
        $.ajax({
            url: 'http://localhost:8080/examPaper/select',
            data: { levelName: this.user.userLevel },
            type: 'get',
            success: function (vo) {
                if (vo.code == 200) {
                    app.examPapers = vo.data;
                    //总页数
                    app.pageSum=vo.data.length%6==0 ? Math.floor(vo.data.length/6) : Math.floor(vo.data.length/6)+1 ;
                }
            }
        });

    },
    methods: {
        choose: function (event) {
            // 获取当前点击对象（a 节点）
            let span = event.currentTarget;
            //先把其他节点的样式还原
            $(span).siblings().find("a").removeClass("title_paper_2");
            //改变当前节点的样式
            $(span).find("a").addClass("title_paper_2");

            let str=$(span).find("a").text();

            if (str == "固定试卷") {
                this.searchPaperType = 1;
            } else if (str == "时段试卷") {
                this.searchPaperType = 2;
            } else if (str == "班级试卷") {
                this.searchPaperType = 3;
            }else{
                this.searchPaperType = null;
            }

            // 查询试卷
            $.ajax({
                url: 'http://localhost:8080/examPaper/select',
                data: { levelName: this.user.userLevel ,subjectName:this.searchSubjectName,paperType:this.searchPaperType},
                type: 'get',
                success: function (vo) {
                    if (vo.code == 200) {
                        app.examPapers = vo.data;
                        // 重置分页
                        app.startNum=0;
                        app.endNum=6;
                        app.currPage=1;
                        //总页数
                        app.pageSum=vo.data.length%6==0 ? Math.floor(vo.data.length/6) : Math.floor(vo.data.length/6)+1 ;
                        $(".btn-next").removeClass("disabled");
                        $(".btn-prev").addClass("disabled");
                        if(app.pageSum==1){
                            $(".btn-next").addClass("disabled");
                        }
                    }
                }
            });

        },
        choose2: function (event) {
            // 获取当前点击对象（a 节点）
            let span = event.currentTarget;
            //先把其他节点的样式还原
            $(span).siblings().find("a").removeClass("title_paper_2");
            //改变当前节点的样式
            $(span).find("a").addClass("title_paper_2");

            let str=$(span).find("a").text();

            if(str=='全部'){
                this.searchSubjectName = null;
            }else{
                this.searchSubjectName = str;
            }
            
            // 查询试卷
            $.ajax({
                url: 'http://localhost:8080/examPaper/select',
                data: { levelName: this.user.userLevel ,subjectName:this.searchSubjectName,paperType:this.searchPaperType},
                type: 'get',
                success: function (vo) {
                    if (vo.code == 200) {
                        app.examPapers = vo.data;
                        // 重置分页
                        app.startNum=0;
                        app.endNum=6;
                        app.currPage=1;
                        //总页数
                        app.pageSum=vo.data.length%6==0 ? Math.floor(vo.data.length/6) : Math.floor(vo.data.length/6)+1 ;
                        $(".btn-next").removeClass("disabled");
                        $(".btn-prev").addClass("disabled");
                        if(app.pageSum==1){
                            $(".btn-next").addClass("disabled");
                        }
                    }
                }
            });

        },
        demo: function (exam) {
            //询问框
            layer.confirm('您即将进行《' + exam.examPaperName + '》答题，是否继续?', {
                icon: 3,
                closeBtn: 0,  // 右上角关闭
                resize: false,  // 是否允许拉伸
                move: false,    // 是否允许拖拽
                btn: ['开始答题', '取消'] //按钮
            }, function (index) {
                layer.close(index);
                sessionStorage.setItem("userTaskId",null);
                sessionStorage.setItem("exam", JSON.stringify(exam));
                sessionStorage.setItem("changeCount", 0);    // 初始化切屏记录数
                $.cookie("djsendtime", null);               //初始化cookie倒计时
                LockButton('#Time', exam.suggestTime);
                window.open("paper_test.html");
            });

        },
        //上一页
        up:function(){
            if(this.currPage==1){
                return;
            }
            if(this.currPage<=this.pageSum){
                $(".btn-next").removeClass("disabled");
            }
            this.startNum=this.startNum-6;
            this.endNum=this.endNum-6;
            this.currPage=this.currPage-1;

            if(this.currPage==1){
                $(".btn-prev").addClass("disabled");
            }
        },
        //下一页
        down:function(){
            if(this.currPage==this.pageSum){
                return;
            }
            if(this.currPage>=1){
                $(".btn-prev").removeClass("disabled");
            }
            this.startNum=this.startNum+6;
            this.endNum=this.endNum+6;
            this.currPage=this.currPage+1;
            if(this.currPage==this.pageSum){
                $(".btn-next").addClass("disabled");
            }
        }
    }
});